package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
public class RabbitController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMQ")
    public String sendMQ(@RequestParam(value = "number", defaultValue = "10") Integer number) {
        System.out.println("main" + Thread.currentThread().getName());
        for (int i = 0; i < number; i++) {
            //如果发送的消息是个对象，使用序列化机制 Serializable
            OrderReturnReasonEntity entity = new OrderReturnReasonEntity();
            entity.setId((long) i);
            entity.setName("haha");
            entity.setCreateTime(new Date());
            rabbitTemplate.convertAndSend("hello.java.exchange", "hello.java", entity, new CorrelationData(UUID.randomUUID().toString()));
        }
        for (int i = 0; i < number; i++) {
            //如果发送的消息是个对象，使用序列化机制 Serializable
            OrderEntity entity = new OrderEntity();
            entity.setId((long) i);
            entity.setCreateTime(new Date());
            rabbitTemplate.convertAndSend("hello.java.exchange", "hello2.java", entity, new CorrelationData(UUID.randomUUID().toString()));
        }
        return "完成";
    }
}
